package pool.errors;

import pool.exceptions.EmptyPoolException;
import pool.test.client.Client;


public class QueueHandlerType extends ErrorHandler {
	
	
	@Override
	public void handle(String handlerType, Client client) throws EmptyPoolException {
		if(handlerType.equals("queue")){
			System.out.println("adding to the queue client " + client.getClientName() + " ...");
			ClientQueue queue = ClientQueue.getInstance();
			queue.add(client);
			
		}else if(next != null){
			next.handle(handlerType, client);
		}
	}
	
	@Override
	public void event(String handlerType) {
		ClientQueue queue = ClientQueue.getInstance();
		
		Client c = queue.poll();
		if(c != null){
			
			/** c.start(); **/
			
			/*
			 * start() can't be called twice
			 * WORK AROUND
			 * only for this example...
			 */
			String name = c.getClientName();
			c = new Client(name);
			c.start();
			
		}
		
	}

}
